// pages/user/upgrade/upgrade.js

var api = require('../../../api.js');
var mta = require('../../../mta_analysis.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    judgeIos:true,
    changMoney:true,
    integral:0,
    integral2money:0,
    settlementOff:false,
    settlementOn:false,
    submitChangeClass:false,
    submitChangeMember:false,
    judge:false,
    judgeT:true,
    newmoney: 0,
    use_integral: 0,
    lesson_newmoney: 0,
    lesson_integral: 0,
    totalMoney:0,
    lesson_totalMoney:0,
    switchValue:false,
    submitChange:true,
    switchChangeCheck:false,
    switchClick:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    mta.Page.init();
    var page = this ;

    app.bind();

    wx.getNetworkType({
      success: function (res) {
        console.log(res)
        page.setData({
          networkType: res.networkType
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDetail();
    var equipmentSystem = wx.getStorageSync('equipmentSystem');
    this.setData({
      equipmentSystem: equipmentSystem
    });
    this.judgeIos();
    // this.onClick();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var page = this;

    page.getDetail();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };  
  },

  //判断ios是否可以支付
  judgeIos: function () {
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var page = this;
    wx.request({
      url: api.passport.is_ios,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token
      },

      success: function (res) {
        console.log(res);
        
          if(res.data.code == 1){
            page.setData({
              judgeIos:false
            })

            console.log(page.data.judgeIos)
          }
        },
      

    });
  },

  //获取详情
  getDetail:function(){
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var page = this;
    wx.request({
      url: api.userdd.vipIndex,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          page.setData({
            user_info:res.data.data.user_info,
            level_list:res.data.data.level_list,
            lesson_list:res.data.data.lesson_list,
            banner_list:res.data.data.banner_list
          })
          console.log(page.data.user_info)
          console.log(page.data.level_list)
          console.log(page.data.lesson_list)
          
          for (var i = 0; i < page.data.level_list.length; i++) {
            console.log(page.data.level_list[i]);
            page.data.level_list[i].choiceDetail = 1;
            console.log(page.data.level_list)
          }
          page.setData({
            level_list:page.data.level_list
          })

          for (var i = 0; i < page.data.lesson_list.length; i++) {
            console.log(page.data.lesson_list[i]);
            page.data.lesson_list[i].choiceClass = true;
           
          }
          page.setData({
            lesson_list: page.data.lesson_list
          })
          console.log(page.data.lesson_list)
        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        }

        
        
      }
    });
  },

  //选择会员
  choice:function(res){
    var page = this ; 
    console.log(res);
    
    page.setData({
      settlementOn: false,
      judge:true,
      judgeT:true,
      submitChange: true,
      switchChangeCheck: false,
      newmoney: 0,
      submitChangeMember:true,
      submitChangeClass: false,
      changMoney : false,
      switchClick:true
    })
    var index = res.currentTarget.dataset.index;
    console.log(index);
    for (var i = 0; i < page.data.level_list.length; i++){
      if (res.currentTarget.dataset.index == i){
        page.data.level_list[i].choiceDetail = 0;
      } else {
        page.data.level_list[i].choiceDetail = 1;
      }
    }
    page.setData({
      level_list: page.data.level_list
    })

    for (var i = 0; i < page.data.lesson_list.length; i++) {
      page.data.lesson_list[i].choiceClass = true;
    }
    page.setData({
      lesson_list: page.data.lesson_list
    })

    var id = page.data.level_list[index].id;

    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var page = this;
    wx.request({
      url: api.vipOrder.submitPreview,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id:id
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          page.setData({
            member_user:res.data.data.user,
            money: res.data.data.user.integral2money,
            integral: res.data.data.user.integral,
            price: res.data.data.level_info.price,
            id:res.data.data.level_info.id
          })
          page.getTotalMoney(page.data.integral);
          console.log()
        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        }



      }
    });

  },

  //会员开关选择器
  switchChangeMember: function (e) {
    var page = this;
    console.log(e);
    if (page.data.integral == undefined){
      wx.showToast({
        title: '请先选择项目',
        icon: 'none',
      })
      page.setData({
        totalMoney: 0
      })
    }else{
      page.setData({
        switchValue: e.detail.value
      })
      if (page.data.switchValue == true) {
        page.setData({
          newmoney: page.data.money,
          use_integral: page.data.integral,
        })
      } else {
        page.setData({
          newmoney: 0,
          use_integral: 0
        })
      }
      page.getTotalMoney();
    }
    
    console.log(page.data.integral);
    console.log(e.detail.value);
    console.log(page.data.newmoney);
    console.log(page.data.use_integral);
    
  },
   
  

  //会员总价格
  getTotalMoney: function () {
    var page = this;
    var user_newmoney = page.data.newmoney;

    var price = page.data.price;
    console.log(user_newmoney)
    console.log(page.data.newmoney)
    console.log(page.data.price)
    var totalMoney = parseFloat(price)*100 - parseFloat(user_newmoney)*100;
    var newTotalMoney = parseFloat(totalMoney)/100
    page.setData({
      totalMoney: newTotalMoney
    })
    console.log(page.data.totalMoney)
  },

  //会员结算
  submitInfo: function (e) {
    console.log(11111)
    var page = this;
    var access_token = wx.getStorageSync('access_token');
    page.getDetail();
    page.getTotalMoney();
    console.log(page.data.use_integral)
    console.log(page.data.id)

    page.setData({
      settlementOn: true
    })


    wx.request({
      url: api.vipOrder.submit,
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: page.data.id,
        num: 1,
        use_integral: page.data.use_integral,
        form_id: e.detail.formId
      },

      success: function (res) {
        console.log(res);
        var data = res.data.data
        if (res.data.code == 0) {
          if (res.data.msg == "订单支付成功") {
            
            wx.showToast({
              title: '恭喜您，购买成功',
              icon: 'none',
            })
            page.getDetail();
          } else  {
            //发起支付
            wx.requestPayment({
              timeStamp: data.timeStamp,
              nonceStr: data.nonceStr,
              package: data.package,
              signType: data.signType,
              paySign: data.paySign,
              success: function (res) {
                console.log(res)
                page.setData({
                  judge: true,
                  change: true,
                })
                page.getDetail();
              },
              fail: function (res) {

              },
              complete: function (res) {

              },

            })
          }

        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        } else if (res.data.code == 1){
          wx.showToast({
            title: '请先选择项目',
            icon: 'none',
          }),
          page.setData({
            totalMoney:0
          })
        }

      }
    });
  },

  //选择专栏
  choiceClick:function(res){
    var page = this;
    console.log(res);
    page.setData({
      settlementOff: false,
      judge: true,
      judgeT: false,
      submitChange:false,
      switchChangeCheck:false,
      lesson_newmoney:0,
      submitChangeClass:true,
      submitChangeMember: false,
      changMoney:false,
      switchClick:true
    })
    var index = res.currentTarget.dataset.index;
    console.log(index);
    for (var i = 0; i < page.data.lesson_list.length; i++) {
      if (res.currentTarget.dataset.index == i) {
        page.data.lesson_list[i].choiceClass = false;
      } else {
        page.data.lesson_list[i].choiceClass = true;
      }
    }
    page.setData({
      lesson_list: page.data.lesson_list
    })

    for (var i = 0; i < page.data.level_list.length; i++) {
      page.data.level_list[i].choiceDetail = 1;
    }
    page.setData({
      level_list: page.data.level_list
    })

    var lesson_id = page.data.lesson_list[index].id;
    console.log(lesson_id)

    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var page = this;
    wx.request({
      url: api.lessonOrder.submitPreview,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: lesson_id
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          page.setData({
            user: res.data.data.user,
            classmoney: res.data.data.user.integral2money,
            integral: res.data.data.user.integral,
            lesson_price: res.data.data.lesson_info.price,
            id: res.data.data.lesson_info.id
          })
          page.lessongetTotalMoney();

        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        }



      }
    });
  },

  //专栏开关选择器
  switchChange: function (e) {
    var page = this;
    console.log(e);
    
    page.setData({
      switchChangeT: e.detail.value
      })
    if (page.data.switchChangeT == true) {
      page.setData({
        lesson_newmoney: page.data.classmoney,
        lesson_integral: page.data.integral,
      })
    } else {
      page.setData({
        lesson_newmoney: 0,
        lesson_integral: 0
      })
    }
    console.log(page.data.integral);
    console.log(e.detail.value);
    console.log(page.data.lesson_newmoney);
    console.log(page.data.lesson_integral);
    page.lessongetTotalMoney();
  },



  //专栏总价格
  lessongetTotalMoney: function () {
    var page = this;
    var lesson_newmoney = page.data.lesson_newmoney;

    var lesson_price = page.data.lesson_price;
    console.log(lesson_newmoney)
    console.log(page.data.newmoney)
    console.log(page.data.lesson_price)
    var lesson_totalMoney = (parseFloat(lesson_price)*100 - parseFloat(lesson_newmoney)*100)/100
    page.setData({
      lesson_totalMoney: lesson_totalMoney
    })
    console.log(page.data.lesson_totalMoney)
  },

  //专栏结算
  submitInfoT: function (e) {
    console.log(222)
    var page = this;
    var access_token = wx.getStorageSync('access_token');
    console.log(page.data.lesson_integral)
    page.lessongetTotalMoney();
    // page.getDetail();
    console.log(page.data.id)

    page.setData({
      settlementOff: true,
    })

    wx.request({
      url: api.lessonOrder.submit,
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: page.data.id,
        num: 1,
        use_integral: page.data.lesson_integral,
        form_id: e.detail.formId
      },

      success: function (res) {
        console.log(res);
        var data = res.data.data
        if (res.data.code == 0) {
          if (res.data.msg == "订单支付成功") {
            page.setData({
              judge: true,
              change: true,
            })
            wx.showToast({
              title: '恭喜您，购买成功',
              icon: 'none',
            })
            page.getDetail();
          } else {
            //发起支付
            wx.requestPayment({
              timeStamp: data.timeStamp,
              nonceStr: data.nonceStr,
              package: data.package,
              signType: data.signType,
              paySign: data.paySign,
              success: function (res) {
                console.log(res)
                page.setData({
                  judge: true,
                  change: true,
                })
                // page.getDetail();
              },
              fail: function (res) {

              },
              complete: function (res) {

              },

            })
          }

        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        } else if (res.data.code == 1) {
          wx.showToast({
            title: '请先选择项目',
            icon: 'none',
          }),
            page.setData({
              totalMoney: 0
            })
        }

      }
    });
  },

  //点击查看详情
  detailBtn:function(res){
    console.log(res)
    var index = res.currentTarget.dataset.index;
    var proList = this.data.level_list;
    let str = JSON.stringify(proList[index]);
    console.log(str);
    wx.navigateTo({
      url: '/pages/memberDetail/memberDetail?str=' + str,
    })
  },



  //跳转网页
  // onClick:function(){
  //   console.log(2222222222)
  //   wx.navigateTo({
  //     url: '/pages/ios/ios',
  //   })
  // }

  
  
  
})